﻿ /*
加解密类
用于loadvars通讯
*/
import Funs
class XXX
{
	//------------- 属性定义
	[Inspectable(defaultValue="abcdefghijklmnopqrstuvwxyz1234567890_~!@$^*:()-+=./")]
	private var Words : String = "abcdefghijklmnopqrstuvwxyz1234567890_~!@$^*:()-+=./";
	private var RS : LoadVars ;
	[Inspectable(defaultValue="5")]
	private var getTimes : Number = 5;
	[Inspectable(defaultValue="http://")]
	private var url : String = "";
	private var interTime : Number = 0;
	private var me : Object ;
	[Inspectable(defaultValue="_root")]
	public var varRoot : String = "";
	[Inspectable(defaultValue={})]
	public var setVar : Array;
	[Inspectable(defaultValue={})]
	public var getVar : Array;
	private var ok : Boolean = false;
	//------------- 构造函数
	public function XXX (word : String)
	{
		if (word != undefined)
		{
			Words = word;
		}
		me = this;
		if (url != "" && url != "http://")
		{
			init ();
		}else
		{
			say ("没有加载地址.")
		}
	}
	//-------------功能函数
	private function init ()
	{
		interTime = setInterval (getRSfun, getTimes, me);
	}
	public function getRSfun (mee)
	{
		if (mee == undefined)
		{
			mee = this;
		}
		mee.say (mee.Words)
		mee.Words = Funs.randomString (mee.Words);
		mee.RS = new LoadVars ();
		say (mee.url + "?word=" + mee.Words );
		mee.RS.load (mee.url + "?word=" + mee.Words + "&___=" + (int (Math.random () * 1000)).toString ());
		mee.RS.onLoad = function (success)
		{
			//mee.say("成功还是失败就看success="+success)
			if (success)
			{
				(mee.setDATA (this)) ? mee.ok = true : mee.ok = false;
			} else
			{
				mee.ok = false;
			}
		}
		clearInterval (mee.interTime);
	}
	private function setDATA (r : LoadVars) : Boolean
	{
		if (getVar.length <= 0 || setVar.length <= 0)
		{
			say ("没有设置获得变量.")
			return false;
		}
		for (var i = 0; i < getVar.length; i ++)
		{
			var rstj : String = r [getVar [i]];
			var key : Array = rstj.split (" ");
			say (rstj);
			eval (varRoot)[setVar [i]] = reBuild (key);
		}
		return true;
	}
	private function reBuild (key : Array)
	{
		var rs : String = "";
		for (var i = 0; i < key.length; i ++)
		{
			rs += Words.charAt (Number (key [i]));
		}
		say ("重建后得到=" + rs);
		return rs;
	}
	private function say (obj : Object)
	{
		trace ("XXX:" + obj);
	}
}
